home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / toolbox / polyfit < prev    next >
Text File  |  1996-04-09  |  809b  |  36 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. //  Syntax: polyfit(x, y, n)
  4.  
  5. //  Description:
  6.  
  7. //  Polynomial curve fitting
  8. //
  9.  
  10. //  polyfit(x,y,n) finds the coefficients of a polynomial
  11. //    formed from the data in vector x of degree n that fits
  12. //    the data in vector y in a least-squares sense.  
  13. //  
  14. //  polyfit returns the coefficients of the n-th order polynomial
  15. //  (in decending powers of x).
  16. //
  17. //  See Also: poly, polyval, roots
  18. //
  19. //-------------------------------------------------------------------//
  20.  
  21. polyfit = function(x, y, n)
  22. {
  23.    local(x)
  24.    
  25.    x = x[:];
  26.    m = x.nr;
  27.    if (m != y.nr) {
  28.       error("row dim. of 1st argument must match row dim. of 2nd argument");
  29.    }
  30.    a[;n+1] = ones(m,1);
  31.    for (i in n:1:-1) {
  32.       a[;i] = a[;i+1].*x;
  33.    }
  34.    return a\y;
  35. };
  36.